﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class BillingEntityBLL : IBaseInfo
    {
        private static readonly IBillingEntityDA BillingEntityDA = DataAccess.CreateBillingEntityDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;

        public BillingEntityBLL()
        {
            this.pubParasInfo = null;
        }

        public BillingEntityBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public BillingEntityBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string BillingImportByData(string[][] ImportByData, string IsUpdate)
        {
            string billingEntityIDTrue = string.Empty;
            string billingEntityIDFalse = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 1; ImportByData.Length > i; i++)
                {
                    BillingEntityInfo billingEntity = new BillingEntityInfo();
                    for (int n = 0; ImportByData[i].Length > n; n++)
                    {
                        try
                        {
                            switch (ImportByData[0][n])
                            {
                                case "计费实体代码":
                                {
                                    if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                    {
                                        billingEntity.BillingEntityID = ImportByData[i][n];
                                    }
                                    continue;
                                }
                                case "计费实体名称":
                                {
                                    billingEntity.Company = ImportByData[i][n];
                                    continue;
                                }
                                case "描述":
                                {
                                    billingEntity.Description = ImportByData[i][n];
                                    continue;
                                }
                                case "增值税号":
                                {
                                    billingEntity.Vat = ImportByData[i][n];
                                    continue;
                                }
                                case "城市":
                                {
                                    billingEntity.City = ImportByData[i][n];
                                    continue;
                                }
                                case "省份":
                                {
                                    billingEntity.State = ImportByData[i][n];
                                    continue;
                                }
                                case "邮编":
                                {
                                    billingEntity.Zip = ImportByData[i][n];
                                    continue;
                                }
                                case "国家":
                                {
                                    billingEntity.Country = ImportByData[i][n];
                                    continue;
                                }
                                case "ISO国家码":
                                {
                                    billingEntity.CountryCode = ImportByData[i][n];
                                    continue;
                                }
                                case "地址行":
                                {
                                    billingEntity.Address = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人1":
                                {
                                    billingEntity.Contact1 = ImportByData[i][n];
                                    continue;
                                }
                                case "电话1":
                                {
                                    billingEntity.Phone1 = ImportByData[i][n];
                                    continue;
                                }
                                case "传真1":
                                {
                                    billingEntity.Fax1 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人1电子邮件":
                                {
                                    billingEntity.Email1 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人2":
                                {
                                    billingEntity.Contact2 = ImportByData[i][n];
                                    continue;
                                }
                                case "电话2":
                                {
                                    billingEntity.Phone2 = ImportByData[i][n];
                                    continue;
                                }
                                case "传真2":
                                {
                                    billingEntity.Fax2 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人2电子邮件":
                                {
                                    billingEntity.Email2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B城市":
                                {
                                    billingEntity.BCity = ImportByData[i][n];
                                    continue;
                                }
                                case "B省份":
                                {
                                    billingEntity.BState = ImportByData[i][n];
                                    continue;
                                }
                                case "B邮编":
                                {
                                    billingEntity.BZip = ImportByData[i][n];
                                    continue;
                                }
                                case "B国家":
                                {
                                    billingEntity.BCountry = ImportByData[i][n];
                                    continue;
                                }
                                case "BISO国家码":
                                {
                                    billingEntity.BCountryCode = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单地址":
                                {
                                    billingEntity.BAddress = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单联系人1":
                                {
                                    billingEntity.BContact1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电话1":
                                {
                                    billingEntity.BPhone1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B传真1":
                                {
                                    billingEntity.BFax1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电子邮件1":
                                {
                                    billingEntity.BEmail1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单联系人2":
                                {
                                    billingEntity.BContact2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电话2":
                                {
                                    billingEntity.BPhone2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B传真2":
                                {
                                    billingEntity.BFax2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电子邮件2":
                                {
                                    billingEntity.BEmail2 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段1":
                                {
                                    billingEntity.Udf1 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段2":
                                {
                                    billingEntity.Udf2 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段3":
                                {
                                    billingEntity.Udf3 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段4":
                                {
                                    billingEntity.Udf4 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段5":
                                {
                                    billingEntity.Udf5 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段6":
                                {
                                    billingEntity.Udf6 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段7":
                                {
                                    billingEntity.Udf7 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段8":
                                {
                                    billingEntity.Udf8 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段9":
                                {
                                    billingEntity.Udf9 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段10":
                                {
                                    billingEntity.Udf10 = ImportByData[i][n];
                                    continue;
                                }
                                case "备注":
                                {
                                    billingEntity.Remark = ImportByData[i][n];
                                    continue;
                                }
                                case "创建人":
                                {
                                    billingEntity.CreatedBy = ImportByData[i][n];
                                    continue;
                                }
                            }
                        }
                        catch
                        {
                            if (!string.IsNullOrEmpty(billingEntity.BillingEntityID))
                            {
                                billingEntityIDFalse = billingEntityIDFalse + billingEntity.BillingEntityID + PublicMethod.GetInstance().GetGlobalMsgResource("C003", this.pubParasInfo.UserLanguage) + "<br/>";
                            }
                        }
                    }
                    billingEntity.IsActive = "Y";
                    billingEntity.UpdatedBy = billingEntity.CreatedBy;
                    if (!string.IsNullOrEmpty(billingEntity.BillingEntityID))
                    {
                        if (BillingEntityDA.CheckBillingEntityIDUnique(dataBase, tran, billingEntity.BillingEntityID, this.dataBaseID[0]))
                        {
                            if (!BillingEntityDA.CheckAuthorizationBillingEntityUserID(dataBase, tran, billingEntity.BillingEntityID, this.pubParasInfo, this.dataBaseID[0]))
                            {
                                billingEntityIDFalse = billingEntityIDFalse + billingEntity.BillingEntityID + PublicMethod.GetInstance().GetGlobalMsgResource("C008", this.pubParasInfo.UserLanguage) + "<br/>";
                            }
                            else if (IsUpdate == "Y")
                            {
                                if (BillingEntityDA.UpdateBillingEntity(dataBase, tran, billingEntity, this.dataBaseID) > 0)
                                {
                                    billingEntityIDFalse = billingEntityIDFalse + billingEntity.BillingEntityID + PublicMethod.GetInstance().GetGlobalMsgResource("C004", this.pubParasInfo.UserLanguage) + "<br/>";
                                }
                            }
                            else
                            {
                                billingEntityIDFalse = billingEntityIDFalse + billingEntity.BillingEntityID + PublicMethod.GetInstance().GetGlobalMsgResource("C005", this.pubParasInfo.UserLanguage) + "<br/>";
                            }
                        }
                        else if (BillingEntityDA.InsertBillingEntity(dataBase, tran, billingEntity, this.dataBaseID) > 0)
                        {
                            billingEntityIDTrue = billingEntityIDTrue + billingEntity.BillingEntityID + ";";
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return billingEntityIDFalse;
        }

        public string Delete(List<string> BillingEntityIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<BillingEntityInfo> billingEntityInfos = new List<BillingEntityInfo>();
                BillingEntityInfo billingEntityInfo = null;
                foreach (string id in BillingEntityIDS)
                {
                    billingEntityInfo = BillingEntityDA.GetBillingEntityByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    billingEntityInfos.Add(billingEntityInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (BillingEntityDA.DeleteBillingEntity(BillingEntityIDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < BillingEntityIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<BillingEntityInfo>(dataBase, tran, billingEntityInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "BILLING_ENTITY", this.dataBaseID);
                        temp = temp + BillingEntityIDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return BillingEntityDA.GetAllBillingEntity(this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetInfoByID(string BillingEntityID)
        {
            return BillingEntityDA.GetBillingEntityByID(BillingEntityID, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            BillingEntityQueryEntity BillingEntityQuery = obj as BillingEntityQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(BillingEntityQuery.BillingEntityID))
            {
                sqlWhere.Add(" B.BILLING_ENTITY_ID " + BillingEntityQuery.BillingEntityID);
            }
            if (!string.IsNullOrEmpty(BillingEntityQuery.Company))
            {
                sqlWhere.Add(" B.COMPANY " + BillingEntityQuery.Company);
            }
            if (!string.IsNullOrEmpty(BillingEntityQuery.City))
            {
                sqlWhere.Add(" B.CITY " + BillingEntityQuery.City);
            }
            if (!string.IsNullOrEmpty(BillingEntityQuery.Address))
            {
                sqlWhere.Add(" B.ADDRESS " + BillingEntityQuery.Address);
            }
            if (!string.IsNullOrEmpty(BillingEntityQuery.IsActive))
            {
                sqlWhere.Add(" B.IS_ACTIVE " + BillingEntityQuery.IsActive);
            }
            return BillingEntityDA.GetBillingEntityByQueryList(sqlWhere, BillingEntityQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            string BillingEntityID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                BillingEntityInfo BillingEntity = obj as BillingEntityInfo;
                if (BillingEntityDA.CheckBillingEntityIDUnique(dataBase, tran, BillingEntity.BillingEntityID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2150_001", this.pubParasInfo.UserLanguage, new List<string> { BillingEntity.BillingEntityID });
                }
                BillingEntity.UpdatedBy = BillingEntity.CreatedBy;
                BillingEntity.CreatedDate = DateTime.Now;
                BillingEntity.UpdatedDate = DateTime.Now;
                if (BillingEntityDA.InsertBillingEntity(dataBase, tran, BillingEntity, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<BillingEntityInfo>(dataBase, tran, null, BillingEntity, AuditAction.Insert, this.pubParasInfo.UserID, "BILLING_ENTITY", this.dataBaseID);
                    BillingEntityID = BillingEntity.BillingEntityID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return BillingEntityID;
        }

        public string Update(object obj)
        {
            string BillingEntityID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                BillingEntityInfo BillingEntity = obj as BillingEntityInfo;
                BillingEntity.UpdatedDate = DateTime.Now;
                BillingEntityInfo oldBillingEntity = BillingEntityDA.GetBillingEntityByID(dataBase, tran, BillingEntity.BillingEntityID, this.dataBaseID[0], this.pubParasInfo);
                if (BillingEntityDA.UpdateBillingEntity(dataBase, tran, BillingEntity, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<BillingEntityInfo>(dataBase, tran, oldBillingEntity, BillingEntity, AuditAction.Update, this.pubParasInfo.UserID, "BILLING_ENTITY", this.dataBaseID);
                    BillingEntityID = BillingEntity.BillingEntityID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return BillingEntityID;
        }
    }
}

